from pickle import GET

from flask import Blueprint, g, jsonify
from flask_restful import Resource, Api, reqparse, fields, abort, marshal

from apps.models.moey_model import WalletMoney
from apps.utils.decorator import login_required
from exts import db

chaxun_bp = Blueprint('chaxun', __name__, url_prefix='/chaxun')

api = Api(chaxun_bp)

parser = reqparse.RequestParser()
parser.add_argument('chaxun', type=str, required=True, help='查询参数必有')


chaxun_fileds = {
    # 钱包 id
    'id': fields.Integer,
    # 钱包地址
    'money_localtion': fields.String,
    # 货币A
    'moneyA': fields.Float,
    # 货币B
    'moneyB': fields.Float,
}


class ChaXunResource(Resource):
    @login_required
    def get(self):
        user = g.user
        if user:
            # 20A=1B
            args = parser.parse_args()
            chaxun = args.get('chaxun')
            if chaxun == '查询':
                money = WalletMoney.query.filter(WalletMoney.user_id == user.id).first()
                if money:
                    data = {
                        'status': 200,
                        'msg': '查询成功',
                        'money': marshal(money, chaxun_fileds)
                    }
                    return jsonify(data)

            else:
                abort(400, msg='参数有误')


api.add_resource(ChaXunResource, '/')
